package gob.cgr.web.sgrh.util;

import java.io.File;
import java.io.IOException;

import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPException;
import it.sauronsoftware.ftp4j.FTPFile;
import it.sauronsoftware.ftp4j.FTPIllegalReplyException;

public class FTPUtil {

	private FTPClient client;
	

	private static String FTP_HOSTNAME="ftp.byethost7.com";
	private static String FTP_LOGIN="b7_12521076";
	private static String FTP_PASSWORD="isgerh";
	private static String FOLDER_LEGAJOS="htdocs,sigerhdata,legajos";
	public FTPUtil() {
		// TODO Auto-generated constructor stub
		client=new FTPClient();
	}
	
	public FTPClient getClient() {
		return client;
	}


	public void setClient(FTPClient client) {
		this.client = client;
	}
	public boolean connect(){
		boolean result=false;
		try {
			client.connect(FTP_HOSTNAME);
			client.login(FTP_LOGIN,FTP_PASSWORD);
			result=true;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return result;
	}
	
	public boolean guardarLegajo(File legajo,String name,String folder){
		boolean result=false;
		try {
			System.out.println("guardando el legajo "+legajo);
			changeDirectoryLegajo();
			client.changeDirectory(folder);
			client.upload(legajo);
			System.out.println("legajo guardado");
			if(name!=null){
				client.rename(legajo.getName(), name);
			}
			
			
			result=true;
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("No se pudo subir el legajo "+e.getMessage());
		}
		return result;
	}
	
	public boolean renameLegajo(String oldName, String newName,String folder){
		boolean result=false;
	try {
		changeDirectoryLegajo();
		client.rename(oldName, newName);
		result=true;
	} catch (Exception e) {
		// TODO: handle exception
		System.out.println("no se pudo renombrar "+e.getMessage());
	}
		return result;
	}
	
	public void disconnect(){
		try {
			client.disconnect(true);
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("error al desconectar "+e.getMessage());
		}
		
	}
	
	public boolean crearCarpeta(String folder){
		boolean result=false;
		try {
			changeDirectoryLegajo();
			boolean hallado=false;
			for(String s:client.listNames()){
				if(s.equals(folder)){
					hallado=true;
					break;
				}
			}
			if(hallado==false){
				client.createDirectory(folder);
			}
			result=true;
			
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("error al crear la carpeta "+e.getMessage());
		}
		return result;
	}
	
	
	public static void main(String[] args){
		
		FTPUtil client=new FTPUtil();
		client.connect();
		try {
			System.out.println(	client.getClient().currentDirectory());
			client.getClient().changeDirectory("htdocs");
			client.getClient().changeDirectory("sigerhdata");
			
			System.out.println(	"2"+client.getClient().currentDirectory());
			File file=new File("D:\\datajboss\\legajos\\0000000001\\CON001.pdf");
			System.out.println("subiendo el file "+file);
			client.getClient().upload(file);
			for(FTPFile f:client.getClient().list())  {
				System.out.println(f.getName());
			}
			System.out.println(""+	client.getClient().currentDirectory());
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("error "+e.getMessage());
		}
	client.disconnect();
	}
	
	public boolean removeLegajo(String folder, String name){
		boolean result=false;
		try {
			
			changeDirectoryLegajo();
			client.changeDirectory(folder);
			client.deleteFile(name);
			result=true;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return result;
		
	}
	
	private void changeDirectoryLegajo() throws  Exception{
		if(!client.currentDirectory().endsWith(FOLDER_LEGAJOS.replace(",", "/"))){
		String[] folders=FOLDER_LEGAJOS.split(",");
		for(String s:folders){
			client.changeDirectory(s);
		}
		System.out.println("FTP: posicionado en la carpeta de legajos");
		}
		else{
			System.out.println("FTP: ya esta posicionado en legajos");
		}
	}
}
